import Variable from "../pages/constants/weather";
import { HttpMethod } from "../pages/types/enum";
import { ApifoxModel } from "../pages/types/weather";


export default class RequestAxios {
  static get<T>(url:string,params?:object){
    return request<T>(url,HttpMethod.GET,params)
  }
  static post(){}
  static put(){}
  static delete(){}
}

function request <T>(url:string,method:HttpMethod,data?:object){
  return new Promise<T>((resolve,reject)=>{
    wx.request<ApifoxModel<T>>({
      url:Variable.BASE_URL + url,
      method,
      data,
      success(res){
        if(res.data.code === 10000){
          resolve(res.data.data)
        }else{
          wx.showToast({
            title:res.data.message,
            icon:'error'
          })
          reject(new Error(res.data.message))
        }
      },
      fail(res){
        reject(new Error(res.errMsg))
      }
    })
  })
}